<template>
 <a-modal
    :visible="visible"
    :title="t('info')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel(false)"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
    
      <a-form-item :label="t('higher')">
        <a-tree-select disabled
          v-model:value="formState.parent"
          style="width: 100%;"
          :dropdown-style="{ maxHeight: '360px', overflow: 'auto' }"
          :tree-data="state.powers"
          :placeholder="t('higher power')"
          replace
          tree-default-expand-all
          :fieldNames="fieldNames" 
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item ref="title" :label="t('title')" name="title">
        <a-input disabled v-model:value="formState.title" />
      </a-form-item> 
      <a-form-item  ref="path" :label="t('path')" name="path">
        <a-input disabled v-model:value="formState.path" />
      </a-form-item>
      <a-form-item   ref="name" :label="t('name')" name="name">
        <a-input disabled v-model:value="formState.name" />
      </a-form-item>
      <a-form-item  :label="t('icon')"  ref="icon" >
        <go2-icon-picker v-model="formState.icon"></go2-icon-picker>
      </a-form-item>
      <a-form-item ref="i18n" :label="t('i18n')" name="i18n">
        <a-input disabled v-model:value="formState.i18n" />
      </a-form-item>
      <a-form-item ref="type" :label="t('type')" name="type">
        <a-select disabled v-model:value="formState.type">
          <a-select-option value="dir"> 目录 </a-select-option>
          <a-select-option value="menu"> 菜单 </a-select-option>
          <a-select-option value="button"> 按钮 </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item ref="sort" :label="t('sort')" name="sort">
        <a-input-number disabled v-model:value="formState.sort" />
      </a-form-item>
      <a-form-item :label="t('status')" name="status">
        <a-switch disabled :checkedValue="1" :unCheckedValue="0" :checkedChildren="t('open')" :unCheckedChildren="t('close')" v-model:checked="formState.status" />
     
      </a-form-item>
      <!-- <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { useI18n  } from "vue-i18n";
import { message } from 'ant-design-vue';
import { add, tree } from "@/core/api/module/permission";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object
    }
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({ 
      depts: [], 
    })
    
    const formRef = ref();
    
    const formState = reactive({
      sort: 0,
      enable: true,
    });

    watch(props,(props) => {
      formState.id = props.record.id;
      formState.title = props.record.title;
      formState.name = props.record.name;
      formState.path = props.record.path; 
      formState.status = props.record.status ; 
      formState.i18n = props.record.i18n;
      formState.type = props.record.type;
      formState.icon = props.record.icon;
      formState.sort = props.record.sort; 
      formState.parent = props.record.parent; 
    })

    const formRules = { 
    };

    const submit = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    const loadPower = () => {
      tree({}).then((response)=>{
        response.data = [{
          id:0,
          title: "顶级菜单",
          children: response.data
        }]
        state.powers = response.data;
      })
    }

    loadPower();

    return {
      t,
      state,
      submit,
      cancel,
      formRef,
      formState,
      formRules,
      
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },

      fieldNames: {children:'children', title:'title', key:'id', value: 'id' }
    };
  },
});
</script>